package com.xujl.mvc;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import com.xujl.mvc.entity.ArgsEntity;
import com.xujl.mvc.utils.StringUtils;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

public class AutoFtl implements IAutomaticGenerated {

    private ArgsEntity argsEntity;
    private Configuration cfg;
    Logger logger = LoggerFactory.getLogger(AutoFtl.class);
    private String username;

    public AutoFtl(ArgsEntity argsEntity) {
        this.argsEntity = argsEntity;

        String source = argsEntity.getRootPath() + "/template";
        logger.error(source);
        cfg = new Configuration(Configuration.getVersion());
        cfg.setClassForTemplateLoading(getClass(), "/template");


        Map<String, String> getenv = System.getenv();

        username = getenv.get("USERNAME");
        String today = DateUtil.format(DateUtil.date().toJdkDate(), "yyyy/MM/dd/ HH/mm/ss");


    }

    @Override
    public void GeneratedXml() {

        try {
            Template template = cfg.getTemplate("/layout_mvc.xml.ftl");
            HashMap<String, String> map = new HashMap<>();
            map.put("packageName", argsEntity.getPagePath());
            map.put("modelclass", argsEntity.getClassName() + "Model");

            String[] stringArray = StringUtils.getStringArray(argsEntity.getClassName());
            StringBuilder builder = new StringBuilder(stringArray[stringArray.length - 1].toLowerCase());
            for (int i = 0; i < stringArray.length - 1; i++) {
                builder.append("_" + stringArray[i].toLowerCase());
            }
            argsEntity.setReslayoutname(builder.toString());


            if (!FileUtil.exist(argsEntity.getReslayout())) {

                FileUtil.mkdir(argsEntity.getReslayout());
            }

            File file = new File(argsEntity.getReslayout() + "/" + argsEntity.getReslayoutname() + ".xml");


            if (file.exists()) {
                return;
            }

            OutputStream out = new FileOutputStream(file);

            StringWriter stringWriter = new StringWriter();


            template.process(map, new OutputStreamWriter(out));


            out.close();

        } catch (IOException e) {
            e.printStackTrace();
        } catch (TemplateException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void GeneratedModel() {
        try {
            logger.error(System.currentTimeMillis() + "");
            String modelPath = argsEntity.getSelectpath() + "/model";
            if (!FileUtil.exist(modelPath)) {
                FileUtil.mkdir(modelPath);
            }

            Template template = null;
            template = cfg.getTemplate("/MvcModel.java.ftl");


            HashMap<String, String> map = new HashMap<>();
            map.put("modelclass", argsEntity.getClassName() + "Model");
            map.put("packageName", argsEntity.getPagePath() + ".model");
            map.put("mainName", argsEntity.getClassName());

            File modelclass = null;
            if (argsEntity.isIskt()) {
                modelclass = new File(modelPath + "/" + map.get("modelclass") + ".kt");
            } else {
                modelclass = new File(modelPath + "/" + map.get("modelclass") + ".java");
            }

            if (modelclass.exists()) {
                return;
            }
            OutputStream out = new FileOutputStream(modelclass);
            template.process(map, new OutputStreamWriter(out));
            out.close();
            logger.error(System.currentTimeMillis() + "");

        } catch (IOException e) {
            e.printStackTrace();
            logger.error(e.getMessage());

        } catch (TemplateException e) {
            e.printStackTrace();
            logger.error(e.getMessage());

        }
    }

    @Override
    public void GeneratedControl() {
        try {
            logger.error(System.currentTimeMillis() + "");
            String modelPath = argsEntity.getSelectpath() + "/control";
            if (!FileUtil.exist(modelPath)) {
                FileUtil.mkdir(modelPath);
            }

            Template template = null;
            HashMap<String, String> map = new HashMap<>();


            if (argsEntity.getType() == 0) {
                template = cfg.getTemplate("/ActivityMvcControl.java.ftl");
                map.put("bindName", "Activity" + argsEntity.getName() + "Binding");
            } else {
                template = cfg.getTemplate("/FragmentMvcControl.java.ftl");
                map.put("bindName", "Fragment" + argsEntity.getName() + "Binding");
            }

            map.put("controlclass", argsEntity.getClassName() + "Control");
            map.put("packageName", argsEntity.getPagePath() + ".control");
            map.put("mainPagePath", argsEntity.getPagePath());
            map.put("mainName", argsEntity.getClassName());

            File controlclass = null;
            if (argsEntity.isIskt()) {
                controlclass = new File(modelPath + "/" + map.get("controlclass") + ".kt");
            } else {
                controlclass = new File(modelPath + "/" + map.get("controlclass") + ".java");
            }

            if (controlclass.exists()) {
                return;
            }
            OutputStream out = new FileOutputStream(controlclass);
            template.process(map, new OutputStreamWriter(out));
            out.close();
            logger.error(System.currentTimeMillis() + "");

        } catch (IOException e) {
            e.printStackTrace();
            logger.error(e.getMessage());

        } catch (TemplateException e) {
            e.printStackTrace();
            logger.error(e.getMessage());

        }
    }

    @Override
    public void GeneratedView() {
        try {
            logger.error(System.currentTimeMillis() + "");
            String modelPath = argsEntity.getSelectpath() + "/view";
            if (!FileUtil.exist(modelPath)) {
                FileUtil.mkdir(modelPath);
            }

            Template template = null;
            template = cfg.getTemplate("/MvcView.java.ftl");


            HashMap<String, String> map = new HashMap<>();
            map.put("viewclass", argsEntity.getClassName() + "View");
            map.put("packageName", argsEntity.getPagePath() + ".view");
            map.put("mainPackage", argsEntity.getPagePath());
            map.put("mainName", argsEntity.getClassName());
            if (argsEntity.getType() == 0) {
                map.put("bindName", "Activity" + argsEntity.getName() + "Binding");
            } else {
                map.put("bindName", "Fragment" + argsEntity.getName() + "Binding");
            }
            File viewclass = null;
            if (argsEntity.isIskt()) {
                viewclass = new File(modelPath + "/" + map.get("viewclass") + ".kt");
            } else {
                viewclass = new File(modelPath + "/" + map.get("viewclass") + ".java");
            }

            if (viewclass.exists()) {
                return;
            }
            OutputStream out = new FileOutputStream(viewclass);
            template.process(map, new OutputStreamWriter(out));
            out.close();
            logger.error(System.currentTimeMillis() + "");

        } catch (IOException e) {
            e.printStackTrace();
            logger.error(e.getMessage());

        } catch (TemplateException e) {
            e.printStackTrace();
            logger.error(e.getMessage());

        }
    }


}
